//
//  AutoSizeModel.m
//  TouGuApp
//
//  Created by tianshikechuang on 16/7/11.
//  Copyright © 2016年 tianshikechuang. All rights reserved.
//




#import "AutoSizeModel.h"
#import "CommModel.h"


#define OriginOfView(view) (view).bounds.origin
#define SizeOfView(view) (view).bounds.size
#define WidthOfView(view) (view).bounds.size.width
#define HeightOfView(view) (view).bounds.size.height

@implementation AutoSizeModel



//单元格的总体高度
- (CGFloat)totalHeight{

    return 90 + 40 + self.contentImageSize.height + self.contentTextSize.height + 10;
}


//根据字体和字数确定文字text的尺寸
- (CGSize)contentTextSize{
    
    return CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20, [self sizeOfString:self.contentText font:self.contentTextFont].height + 3);
}


//根据图片的数量来确定尺寸
- (CGSize)contentImageSize{
    
    CGSize size = CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20, 1);
    CGFloat gap = (WidthOfView([UIScreen mainScreen]) - 20)/3.0;
    
    
    if (self.contentImages.count > 0) {
        
        size = CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20, gap * 1);

    }else{
        
          size = CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20,  1);
 
    }
    
//    if (self.contentImages.count > 6) {
//        
//        size = CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20, gap * 3);
//        
//    }else if (self.contentImages.count > 3){
//        
//        size = CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20, gap * 2);
//        
//    }else if (self.contentImages.count > 0){
//        
//        size = CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20, gap * 1);
//    }else if (self.contentImages.count == 0){
//    
//          size = CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20,  1);
//        
//    }
    return size;
}


//根据传入text的字体和数量计算其高
- (CGSize)sizeOfString:(NSString *)str font:(UIFont *)font{
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[NSFontAttributeName] = font;
    CGRect rect = [str boundingRectWithSize:CGSizeMake(WidthOfView([UIScreen mainScreen]) - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size;
}

+ (AutoSizeModel *)tranfromByCommModel:(id)model{
 
    CommModel *model1 = (CommModel *)model;
    
    AutoSizeModel *model2 = [[AutoSizeModel alloc] init];
    model2.icon = model1.u_headUrl;
    model2.name = model1.u_name;
    model2.Zang = model1.s_likeNum;
    model2.Pinlun = model1.s_commentNum;
    model2.contentText = model1.p_content;
    model2.isHot = model1.hotBool;
    model2.model = model;
    model2.p_id = model1.p_id;
    
    if (model1.pt_url.length == 0 || [model1.pt_url isKindOfClass:[NSNull class]]) {
        model2.contentImages = @[];
    }else{
       model2.contentImages = model1.pt_url?[model1.pt_url componentsSeparatedByString:@","]:nil;
    }
    model2.p_date = model1.p_date;
    
    return model2;

}

//用户头像
//@property(nonatomic, strong) UIImage * icon;
////用户昵称
//@property(nonatomic, strong) NSString * name;
////是否为热度
//@property(nonatomic, assign) BOOL isHot;
////文字
//@property(nonatomic, strong) NSString * contentText;
////文字字体
//@property(nonatomic, strong) UIFont * contentTextFont;
////图片
//@property(nonatomic, strong) NSArray * contentImages;
////点赞
//@property(nonatomic, strong) NSString * Zang;
////评论
//@property(nonatomic, strong) NSString * Pinlun;
////分享
//@property(nonatomic, strong) NSString * Fenxian;
//// 总个cell的高度
//@property(nonatomic, assign) CGFloat totalHeight;
////整个文字所占尺寸大小
//@property(nonatomic, assign) CGSize contentTextSize;
////图片所占文字大小
//@property(nonatomic, assign) CGSize contentImageSize;


////用户时间
//@property(nonatomic, strong) NSString * p_date;
////用户id
//@property(nonatomic, strong) NSString * p_id;
////昵称
//@property(nonatomic, strong) NSString * u_name;
////点赞
//@property(nonatomic, strong) NSString * s_likeNum;
////评论数
//@property(nonatomic, strong) NSString * s_commentNum;
//// 帖子内容
//@property(nonatomic, assign) NSString * p_content;
////头像url
//@property(nonatomic, assign) NSString * u_headUrl;



@end
